﻿using System.Linq;
using System.ServiceModel;
using CCU.Core.Module;

namespace CCU.Core.Channel
{
    public class ChannelVerify
    {
        public static bool IsAllowAgent(string agentId)
        {
            string constr = Crypto.Decrypt(CCUChannelExtra.GetEntity.userConfig);
            if (!string.IsNullOrEmpty(constr))
            {
                string scope = constr.GetMatch("(?<=,\"GameZones\":)\"([^\"]*)\"");
                if (!string.IsNullOrEmpty(scope))
                {
                    return scope.Split(',').Contains<string>(agentId);
                }
            }
            return false;
        }

        public static bool IsValidAgent(string agentId)
        {
            if (agentId == ConfigManager.AppSettings[OperationContext.Current.Channel.LocalAddress.Uri.Port]["agentId"])
                return true;
            return false;
        }

        public static bool IsValidChannelKey(string moduleKey)
        {
            if (RemoteLoader.moduleObj.ModuleKey == moduleKey)
                return true;
            return false;
        }

        public static bool IsValidChannelInfo(CCUChannelExtra info)
        {
            bool flag = true;

            if (info != null)
            {
                if (string.IsNullOrEmpty(info.gameZoneId))
                {
                    flag = false;
                    CCUChannelExtra.GetEntity.output = "error! targetServerIp is empty.";
                }
                else if (string.IsNullOrEmpty(info.userId))
                {
                    flag = false;
                    CCUChannelExtra.GetEntity.output = "error! userId is empty.";
                }
                else if (string.IsNullOrEmpty(info.userName))
                {
                    flag = false;
                    CCUChannelExtra.GetEntity.output = "error! userName is empty.";
                }
                else if (string.IsNullOrEmpty(info.moduleKey))
                {
                    flag = false;
                    CCUChannelExtra.GetEntity.output = "error! moduleKey is empty.";
                }
                else if (string.IsNullOrEmpty(info.clientIP))
                {
                    flag = false;
                    CCUChannelExtra.GetEntity.output = "error! clientIP is empty.\r\n";
                }
                else if (string.IsNullOrEmpty(info.targetServerIp))
                {
                    flag = false;
                    CCUChannelExtra.GetEntity.output = "error! agentIP is empty.";
                }
                else if (string.IsNullOrEmpty(info.gameZoneName))
                {
                    flag = false;
                    CCUChannelExtra.GetEntity.output = "error! agentName is empty.";
                }
                else if (string.IsNullOrEmpty(info.userConfig))
                {
                    flag = false;
                    CCUChannelExtra.GetEntity.output = "error! userConfig is empty.";
                }
                else if (string.IsNullOrEmpty(info.flowId))
                {
                    flag = false;
                    CCUChannelExtra.GetEntity.output = "error! flowId is empty.";
                }
            }
            else
            {
                CCUChannelExtra.GetEntity.output = "error! CCU ChannelExtraInfo is empty.";
            }
            return flag;
        }
    }
}
